package com.sky.controller.admin;

import com.sky.annotation.dish.DishParam;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.enumeration.OperationType;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "菜品管理")
@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {
    @Resource
    private DishService dishService;

    @PostMapping
    @ApiOperation("新增菜品")
    @DishParam(value = OperationType.INSERT)
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("dishDTO:{}", dishDTO);
//        dishService.saveWithFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 分页条件查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult<DishVO>> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页条件查询:{}", dishPageQueryDTO);
        return Result.success(dishService.page(dishPageQueryDTO));
    }

    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> listByCategoryId(Long categoryId) {
        return Result.success(dishService.getDishListByCategoryId(categoryId));
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> listById(@PathVariable Long id) {
        return Result.success(dishService.getDishListById(id));
    }

    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品状态")
    public Result enableOrDisable(@PathVariable Integer status, Long id) {
        dishService.updateStatus(id, status);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改菜品")
    @DishParam(value = OperationType.UPDATE)
    public Result updateDish(@RequestBody DishDTO dishDTO) {
        dishService.updateDish(dishDTO);
        return Result.success();
    }

    /**
     * 批量删除接口
     * 或者使用@RequestParam List<Long> ids接受参数, SpringMvc会自动转换
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result deleteBatch(String ids) {
        List<Long> idList = dishService.getIdsByString(ids, ",");
        log.info("批量删除菜品:{}", idList);
        dishService.deleteBatch(idList);
        return Result.success();
    }
}
